59. Scheduled tasks module

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

Examples of how to use the Scheduled Tasks module available in Windows 8/Server 2012 and on.

59.1: Run PowerShell Script in Scheduled Task

Creates a scheduled task that executes immediately, then on start up to run C:\myscript.ps1 as SYSTEM

1
2
3
4
5
6
7
8
9
$ScheduledTaskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
$ScheduledTaskTrigger1 = New-ScheduledTaskTrigger -AtStartup
$ScheduledTaskTrigger2 = New-ScheduledTaskTrigger -Once -At $(Get-Date) -RepetitionInterval "00:01:00" -RepetitionDuration $([timeSpan] "24855.03:14:07")
$ScheduledTaskActionParams = @{
  Execute = "PowerShell.exe"
  Argument = '-executionpolicy Bypass -NonInteractive -c C:\myscript.ps1 -verbose >> C:\output.log 2>&1"'
}
$ScheduledTaskAction = New-ScheduledTaskAction @ScheduledTaskActionParams
Register-ScheduledTask -Principal $ScheduledTaskPrincipal -Trigger @($ScheduledTaskTrigger1,$ScheduledTaskTrigger2) -TaskName "Example Task" -Action $ScheduledTaskAction